package com.darren.framework.service.impl;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.darren.framework.dao.SystemParameterDao;
import com.darren.framework.entity.SystemParameter;
import com.darren.framework.service.SystemParameterService;
import com.darren.framework.utils.Constant;
import com.darren.framework.utils.ConvertUtil;

/**
 * <pre>
 * *********************************************
 * Copyright DARREN
 * All rights reserved.
 * Description: TODO(description)
 * HISTORY:
 * *********************************************
 *  Version		Date		Author	   		Desc	
 *   v1.0       2018-04-11  Darren Zhou    Create

 * *********************************************
 * </pre>
 */

@Service
public class SystemParameterServiceImpl extends GenericServiceImpl<SystemParameter,Long> implements SystemParameterService{
	
	@Resource
	private SystemParameterDao systemParameterDao;
	
	@Override
	@Cacheable(value = Constant.CACHE_SYSTEM_PARAMS)
	public List<SystemParameter> getAllSystemParameter(){		
		return systemParameterDao.getAll();
	}
	
	@Override
	@Cacheable(value = Constant.CACHE_SYSTEM_PARAMS, key = "#id")
	public SystemParameter findById(Long id){		
		return systemParameterDao.findById(id);
	}
	
	@Override
	public <T> T getSysParamVal(String lookupCat,String lookupType,Class<T> clazz){
		String lookupValue = getSysParamVal(lookupCat, lookupType);
		return ConvertUtil.castToJavaBean(lookupValue, clazz);
	}
	
	@Override	
	public String getSysParamVal(String lookupCat,String lookupType){
		SystemParameter systemParameter = getSystemParamterByLookupCatAndLookupType(lookupCat, lookupType);
		if(systemParameter == null){
			return null;
		}
		return ConvertUtil.castToJavaBean(systemParameter.getLookupValue(),String.class);
	}
	
	@Override
	@Cacheable(value = Constant.CACHE_SYSTEM_PARAMS, key = "#lookupCat-#lookupType")
	public SystemParameter getSystemParamterByLookupCatAndLookupType(String lookupCat,String lookupType){
		SystemParameter systemParameter = systemParameterDao.getSystemParamter(lookupCat, lookupType);		
		return systemParameter;
	}
	
	/**
	 * 清除系统参数缓存
	 */
	@Override
	@CacheEvict(value=Constant.CACHE_SYSTEM_PARAMS,allEntries=true)
	public void clearSystemParameter(){
		cacheManager.getCache(Constant.CACHE_SYSTEM_PARAMS).removeAll();
		logger.info("clear all system parameters from cache...");
	}
}
